home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / arcers / tar316.zip / BITS.C < prev    next >
Text File  |  1994-07-21  |  2KB  |  90 lines

  1. #include <stdio.h>
  2. #include "modern.h"
  3. #include "stdinc.h"
  4. #include "zalloc.h"
  5. #include "zipdefs.h"
  6. #include "zipguts.h"
  7. #include "zippipe.h"
  8.  
  9.        void (*ziputbyte)__ARGS__((int)) = NULL;
  10. static unsigned bitbuff;
  11. static int boffset;
  12.  
  13. #ifdef DEBUG
  14. ulg bits_sent;   /* bit length of the compressed data */
  15. #endif
  16.  
  17. void bi_init() /* Initialize the bit string routines. */
  18. {
  19.    bitbuff = 0;
  20.    boffset = 0;
  21. #ifdef DEBUG
  22.    bits_sent = 0L;
  23. #endif
  24. }
  25.  
  26. void send_bits(value, length) /* Send a value on a given number of bits. */
  27. unsigned value; /* value to send */
  28. int length;     /* number of bits: length =< 16 */
  29. {
  30. #ifdef DEBUG
  31.    Tracevv((stderr," l %2d v %4x ", length, value));
  32.    Assert(length > 0 && length <= 15, "invalid length");
  33.    Assert(boffset < 8, "bad offset");
  34.    bits_sent += (ulg)length;
  35. #endif
  36.    bitbuff |= value << boffset;
  37.    if ((boffset += length) >= 8) {
  38.       putbyte(bitbuff);
  39.       value >>= length - (boffset -= 8);
  40.       if (boffset >= 8) {
  41.          boffset -= 8;
  42.          putbyte(value);
  43.          value >>= 8;
  44.       }
  45.       bitbuff = value;
  46.    }
  47. }
  48.  
  49. /* Write out any remaining bits in an incomplete byte. */
  50. void bi_windup()
  51. {
  52.    Assert(boffset < 8, "bad offset");
  53.    if (boffset) {
  54.       putbyte(bitbuff);
  55.       boffset = 0;
  56.       bitbuff = 0;
  57. #ifdef DEBUG
  58.       bits_sent = (bits_sent+7) & ~7;
  59. #endif
  60.    }
  61. }
  62.  
  63. void bi_putsh(x)
  64. ush x;
  65. {
  66.    putbyte(x & 255); putbyte((x>>8) & 255);
  67. }
  68.  
  69. /* Copy a stored block to the zip file, storing first the length and its
  70.    one's complement if requested. */
  71. void copy_block(buf, len, header)
  72. char far *buf; /* the input data */
  73. unsigned len;  /* its length */
  74. int header;    /* true if block header must be written */
  75. {
  76.    /* align on byte boundary */
  77.    bi_windup();
  78.  
  79.    if (header) {
  80.       bi_putsh(len); bi_putsh(~len);
  81. #ifdef DEBUG
  82.       bits_sent += 2*16;
  83. #endif
  84.    }
  85.    while (len--) putbyte(*buf++);
  86. #ifdef DEBUG
  87.    bits_sent += (ulg)len<<3;
  88. #endif
  89. }
  90.